<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;

class IndexController extends CommonController {

    public function index(){

        $this->nav = M('type')->select();

        $order = D('OrderRelation')->relation(true)->select();
        // p($order);
        // for($i=0; $i<count($order)-1; $i++){
        //     for($j=1; $j<count($order); $j++){
        //         if(count($order[$i]['menu']) < count($order[$j]['menu'])){
        //             $middle    = $order[$i];
        //             $order[$i] = $order[$j];
        //             $order[$j] = $middle;
        //         }
        //     }
        // }
        for($i=0; $i<count($order); $i++){
            $order[$i]['sum'] = count($order[$i]['menu']);
        }
        //p($order);die;
        $this->userName = cookie('name');
        $this->order = $order;
        $this->display();
    }

    public function index1(){
        $user_id  = cookie('id');
        $date     = date("Y-m-d");
        $where    = array('user_id' => $user_id ,'date' => $date);
        if($data  = M('user_menu')->where($where)->select())
        {
            foreach ($data as $key => $value) {
                $menu_checked_id[$key] = $value['menu_id'];
            }
            $flag = true;
        }else{
            $flag = false;
        }
        $name       = cookie('name');
    	$meal       = D('TypeMenuRelation')->relation(true)->select();
        //p($meal);die;
        for($i=0;$i<count($meal);$i++)
        {
            for($j=0;$j<count($meal[$i]['menu']);$j++)
            {
                $meal[$i]['menu'][$j]['checked'] = 0;
                for($k=0;$k<count($menu_checked_id) && $flag == true;$k++){
                    if($meal[$i]['menu'][$j]['id'] == $menu_checked_id[$k]){
                        $meal[$i]['menu'][$j]['checked'] = 1;
                    }
                }
                
            }
        }
	//p($meal);die;
        $this->meal = $meal;
        $this->name = $name;
    	$this->display();
	}

    public function bookHandle(){
        $menu      = I('meal');
        $user_id   = cookie('id');
        $date      = date("Y-m-d");
        if($menu == null){
            $this->error('请选择食物');
        }
        $where = array('user_id' => $user_id, 'date' => $date);
        if(M('user_menu')->where($where)->find()){
            M('user_menu')->where($where)->delete();
        }
        foreach ($menu as $value) {
            $data = array(
                'user_id' => $user_id,
                'menu_id' => $value,
                'date'    => $date
            );
            M('user_menu')->add($data);
        }
        $this->success('成功确认',U('Index/index')); 

    }

    public function check(){
        // $this->nav = M('type')->select();
        // $date  = date("Y-m-d");
        // $where = array('date' => $date);
        // $data  = M('user_menu')->where($where)->select();
        // for($i=0;$i<COUNT($data);$i++){
        //     $muser = M('user')->find($data[$i]['user_id']);
        //     $mmenu = M('menu')->find($data[$i]['menu_id']);
        //     $data[$i]['userName'] = $muser['name'];
        //     $data[$i]['menuName'] = $mmenu['name'] . '-' . $mmenu['price'] . '元';
        //     $data[$i]['price']    = $mmenu['price'];
        // }
        // p($data);die;
        $this->nav = M('type')->select();
        $userId  = cookie('id');
        $date    = date("Y-m-d");
        $where   = array('date' => $date);
        $data    = M('user_menu')->where($where)->order('menu_id asc')->select();
        $save    = array();
        $newData = array();
        $orderList = array();
        $k = 0;
        for($i=0; $i<COUNT($data); $i++){
            $user = M('user')->find($data[$i]['user_id']);
            $flag = false;
            for($j=0; $j<COUNT($save); $j++)
            {
                if($save[$j]['menuId'] == $data[$i]['menu_id']){
                    $newData[$save[$j]['arrIndex']]['num']++;
                    $newData[$save[$j]['arrIndex']]['sum'] += $newData[$save[$j]['arrIndex']]['price'];
                    $newData[$save[$j]['arrIndex']]['user'][] = $user['id'];
                    $orderList[$user['id']]++;
                    $newData[$save[$j]['arrIndex']]['user_string'] .= (' ' . $user['name']);
                    $flag = true;
                    break;
                }
            }
            if($flag) continue;
            $menu = M('menu')->find($data[$i]['menu_id']);
            $newData[$k]['name'] = $menu['name'];
            $newData[$k]['price'] = $menu['price'];
            $newData[$k]['num'] = 1;
            $newData[$k]['sum'] = $menu['price'];
            $newData[$k]['user_string'] .= $user['name'];
            $newData[$k]['user'][] = $user['id'];
            $orderList[$user['id']]++;
            $save[COUNT($save)] = array('menuId'=>$data[$i]['menu_id'],'arrIndex'=>$k);
            $k++;
        }
        $totalSum = 0;
        $totalNum = 0;
        $user = M('user')->select();
        $orderUser = array();
        $restUser  = array();
        // 初始化用户
        for($i=0; $i<COUNT($user); $i++){
            $flag = false;
            foreach ($orderList as $key => $value) {
                if($key == $user[$i]['id']){
                    $orderUser[] = $user[$i]['name'];
                    $flag = true;
                    break;
                }
            }
            if(!$flag)
            {
                $restUser[] = $user[$i]['name'];
            }
        }
        // p($orderUser);die;
        // 计算总数和计算已定人员和未定人员
        for($i=0; $i<COUNT($newData); $i++){
            $totalSum += $newData[$i]['sum'];
            $totalNum += $newData[$i]['num'];
            for($j=0; $j<COUNT($newData[$i]['user']); $j++){

            }
        }
        $this->totalNum  = $totalNum;
        $this->totalSum  = $totalSum;
        $this->orderUser = $orderUser;
        $this->restUser  = $restUser;
        $this->meal = $newData;
        $this->userName = cookie('name');
        //p($newData);die;
        $this->display();
    }

    public function myorder(){
        $this->nav = M('type')->select();
        $userId = cookie('id');
        $date   = date("Y-m-d");
        $where = array('date' => $date, 'user_id' => $userId);
        $data  = M('user_menu')->where($where)->order('menu_id asc')->select();
        $save  = array();
        $newData = array();
        $k = 0;
        for($i=0; $i<COUNT($data); $i++){
            $flag = false;
            for($j=0; $j<COUNT($save); $j++)
            {
                if($save[$j]['menuId'] == $data[$i]['menu_id']){
                    $newData[$save[$j]['arrIndex']]['num']++;
                    $newData[$save[$j]['arrIndex']]['sum'] += $newData[$save[$j]['arrIndex']]['price'];
                    $newData[$save[$j]['arrIndex']]['user_menu_id'] = array_merge($newData[$save[$j]['arrIndex']]['user_menu_id'],array($data[$i]['id']));
                    $newData[$save[$j]['arrIndex']]['user_menu_id_string'] .= (','.$data[$i]['id']);
                    $flag = true;
                    break;
                }
            }
            if($flag) continue;
            $menu = M('menu')->find($data[$i]['menu_id']);
            $newData[$k]['name'] = $menu['name'];
            $newData[$k]['price'] = $menu['price'];
            $newData[$k]['num'] = 1;
            $newData[$k]['sum'] = $menu['price'];
            $newData[$k]['user_menu_id'] = array($data[$i]['id']);
            $newData[$k]['user_menu_id_string'] .= $data[$i]['id'];
            $save[COUNT($save)] = array('menuId'=>$data[$i]['menu_id'],'arrIndex'=>$k);
            $k++;
        }
        //p($newData);die;
        $this->userName = cookie('name');
        $this->data = $newData;
        $this->display();
    }

    public function myorderdeleteHandle(){
        $arr = explode(",",I('id'));
        foreach ($arr as $key => $value) {
            M('user_menu')->delete($value);
        }
        $this->success('删除成功');
    }

    public function history(){
        $this->nav = M('type')->select();
        $userId = cookie('id');
        $date   = date("Y-m-d");
        $where = array('user_id' => $userId);
        $data  = M('user_menu')->where($where)->select();
        $save  = array();
        $newData = array();
        $k = 0;
        for($i=0; $i<COUNT($data); $i++){
            $flag = false;
            for($j=0; $j<COUNT($save); $j++)
            {
                if($save[$j]['menuId'] == $data[$i]['menu_id'] && $save[$j]['date'] == $data[$i]['date']){
                    $newData[$save[$j]['arrIndex']]['num']++;
                    $newData[$save[$j]['arrIndex']]['sum'] += $newData[$save[$j]['arrIndex']]['price'];
                    $newData[$save[$j]['arrIndex']]['user_menu_id'] = array_merge($newData[$save[$j]['arrIndex']]['user_menu_id'],array($data[$i]['id']));
                    $flag = true;
                    break;
                }
            }
            if($flag) continue;
            $menu = M('menu')->find($data[$i]['menu_id']);
            $newData[$k]['name'] = $menu['name'];
            $newData[$k]['price'] = $menu['price'];
            $newData[$k]['num'] = 1;
            $newData[$k]['sum'] = $menu['price'];
            $newData[$k]['date'] = $data[$i]['date'];
            $newData[$k]['user_menu_id'] = array($data[$i]['id']);
            $save[COUNT($save)] = array('menuId'=>$data[$i]['menu_id'],'arrIndex'=>$k,'date'=>$data[$i]['date']);
            $k++;
        }
        //p($newData);die;
        $this->userName = cookie('name');
        $this->data = $newData;
        $this->display();
    }

    public function order(){

        $this->nav = M('type')->select();
        $typeId    = I('id');
        $meal      = array();
        $mealAll   = D('TypeMenuRelation')->relation(true)->select();
        for($i=0;$i<COUNT($mealAll);$i++){
            if($mealAll[$i]['id'] == $typeId){
                $meal = $mealAll[$i];
            }
        }
        $this->meal = $meal;
        $this->typeId = $typeId;
        $this->userName = cookie('name');
        // p($meal);die;
        $this->display();
    }

    public function orderHandle(){
        $menuId = I('id');
        $userId = cookie('id');
        $date   = date("Y-m-d");
        if($menuId == null){
            $this->error('请选择食物');
        }
        $data = array(
            'user_id' => $userId,
            'menu_id' => $menuId,
            'date'    => $date
        );
        M('user_menu')->add($data);
        $this->success('成功确认'); 
    }

    public function about(){
        $this->nav = M('type')->select();
        $this->userName = cookie('name');
        $this->display();
    }

}
